<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>注册给定的函数作为 __autoload 的实现</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.spl-autoload-register" class="refentry">
 <div class="refnamediv">
 <h1 class="refname">spl_autoload_register</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7)</p><p class="refpurpose"><span class="refname">spl_autoload_register</span> &mdash; <span class="dc-title">注册给定的函数作为 __autoload 的实现</span></p>

 </div>
  <div class="refsect1 description" id="refsect1-function.spl-autoload-register-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>spl_autoload_register</strong></span>
    ([ <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$autoload_function</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$throw</code><span class="initializer"> = true</span></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$prepend</code><span class="initializer"> = false</span></span>
  ]]] ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
  将函数注册到SPL __autoload函数队列中。如果该队列中的函数尚未激活，则激活它们。
  </p>
  <p class="para">
   如果在你的程序中已经实现了<span class="function"><a href="autoload.html" class="function">__autoload()</a></span>函数，它必须显式注册到<span class="function"><a href="autoload.html" class="function">__autoload()</a></span>队列中。因为
  <span class="function"><strong>spl_autoload_register()</strong></span>函数会将Zend Engine中的<span class="function"><a href="autoload.html" class="function">__autoload()</a></span>函数取代为<span class="function"><a href="spl_autoload.html" class="function">spl_autoload()</a></span>或<span class="function"><a href="spl_autoload_call.html" class="function">spl_autoload_call()</a></span>。
  </p>
  <p class="para">
   如果需要多条 autoload 函数，<span class="function"><strong>spl_autoload_register()</strong></span> 
    满足了此类需求。
   它实际上创建了 autoload 函数的队列，按定义时的顺序逐个执行。相比之下，
   <span class="function"><a href="autoload.html" class="function">__autoload()</a></span> 只可以定义一次。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.spl-autoload-register-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">autoload_function</code></dt>

     <dd>

      <p class="para">
       欲注册的自动装载函数。如果没有提供任何参数，则自动注册 autoload 的默认实现函数<span class="function"><a href="spl_autoload.html" class="function">spl_autoload()</a></span>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">throw</code></dt>

     <dd>

      <p class="para">
       此参数设置了 <code class="parameter">autoload_function</code> 无法成功注册时，
       <span class="function"><strong>spl_autoload_register()</strong></span>是否抛出异常。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">prepend</code></dt>

     <dd>

      <p class="para">
       如果是 true，<span class="function"><strong>spl_autoload_register()</strong></span> 会添加函数到队列之首，而不是队列尾部。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.spl-autoload-register-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>



 <div class="refsect1 changelog" id="refsect1-function.spl-autoload-register-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        引入了命名空间的支持。
       </td>
      </tr>

      <tr>
       <td>5.3.0</td>
       <td>
        添加了 <code class="parameter">prepend</code> 参数。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.spl-autoload-register-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="regexiterator.getpregflags.example.basic">
    <p><strong>Example #1 <span class="function"><strong>spl_autoload_register()</strong></span> 作为 <span class="function"><a href="autoload.html" class="function">__autoload()</a></span> 函数的替代</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;function&nbsp;__autoload($class)&nbsp;{<br />//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include&nbsp;'classes/'&nbsp;.&nbsp;$class&nbsp;.&nbsp;'.class.php';<br />//&nbsp;}<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">my_autoloader</span><span style="color: #007700">(</span><span style="color: #0000BB">$class</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;include&nbsp;</span><span style="color: #DD0000">'classes/'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$class&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'.class.php'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">spl_autoload_register</span><span style="color: #007700">(</span><span style="color: #DD0000">'my_autoloader'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;或者，自&nbsp;PHP&nbsp;5.3.0&nbsp;起可以使用一个匿名函数<br /></span><span style="color: #0000BB">spl_autoload_register</span><span style="color: #007700">(function&nbsp;(</span><span style="color: #0000BB">$class</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;include&nbsp;</span><span style="color: #DD0000">'classes/'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$class&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'.class.php'</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
   <div class="example" id="regexiterator.setflags.example.basic">
    <p><strong>Example #2 class 未能加载的 <span class="function"><strong>spl_autoload_register()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">namespace&nbsp;</span><span style="color: #0000BB">Foobar</span><span style="color: #007700">;<br /><br />class&nbsp;</span><span style="color: #0000BB">Foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">'[['</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">.</span><span style="color: #DD0000">']]'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">spl_autoload_register</span><span style="color: #007700">(</span><span style="color: #0000BB">__NAMESPACE__&nbsp;</span><span style="color: #007700">.</span><span style="color: #DD0000">'\Foo::test'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;自&nbsp;PHP&nbsp;5.3.0&nbsp;起<br /><br /></span><span style="color: #007700">new&nbsp;</span><span style="color: #0000BB">InexistentClass</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
[[Foobar\InexistentClass]]
Fatal error: Class &#039;Foobar\InexistentClass&#039; not found in ...
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.spl-autoload-register-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="autoload.html" class="function" rel="rdfs-seeAlso">__autoload()</a> - 尝试加载未定义的类</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>